package com.tanhua.server.controller;

import com.tanhua.common.pojo.Answers;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.vo.QuestionnaireVo;
import com.tanhua.server.vo.TestResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 测灵魂
 */
@RestController
@RequestMapping("testSoul")
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;


    /**
     * 问卷列表
     * List<QuestionnaireVo>
     * @return
     */
    @GetMapping
    public ResponseEntity SoulQuestionnaire() {
        List<QuestionnaireVo> questionnaireVos = this.testSoulService.SQuestionnaireLike();
        if (questionnaireVos != null){
            return ResponseEntity.ok(questionnaireVos);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * 提交问卷
     *
     * @param answers
     */
    @PostMapping
    public ResponseEntity TestSoul(@RequestBody Map<String, List<Answers>> answers) {
        List<Answers> answersList = answers.get("answers");
        String id = testSoulService.testSoul(answersList);
        return ResponseEntity.ok(id);
    }

    /**
     * 测试结果
     * @param id
     * @return
     */
    @GetMapping("report/{id}")
    public ResponseEntity<TestResultVo> TestResult(@PathVariable("id") Long id) {
        try {
            TestResultVo testResultVo = this.testSoulService.testResult(id);
            return ResponseEntity.ok(testResultVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
